import { controls } from '../../controls';
import type BaseControlService from '../../controls/base/service';
import { conditionType, flowType } from '../../types';
import DI from '../../utils/di';
import Card, { type ICardView } from '../base';

class FlowCardService extends DI implements ICardView {
  title: string = '流转条件';

  flowType = controls.flow.flowType;
  conditionType = controls.flow.conditionType;
  expression = controls.flow.expression;

  visible: boolean = true;

  controls: BaseControlService[] = [
    this.flowType,
    this.conditionType,
    this.expression,
  ];

  component = Card;

  constructor() {
    super();
    // 固定值
    this.conditionType.value = conditionType.expression;
  }

  getLabelWidth() {
    return this.controls.reduce((pre, cur) => {
      return Math.max(pre, cur.getLabelWidth());
    }, 0);
  }

  initEvent() {
    this.flowType.bindOnChange((val) => {
      // 这里执行的时，this不是proxy对象，而panel对象在proxy对象中，这里会获取不到
      // 推测是运行时机的问题， initEvent在构造函数中执行时，proxy对象还没有创建，所以把bindOnChange的执行放到init中执行
      // console.log('flowType.bindOnChange', this);

      this.updateProperties();
      this.updateVisible();
    });

    this.expression.bindOnChange((val) => {
      this.updateProperties();
    });
  }

  updateVisible() {
    this.conditionType.visible = this.flowType.value === flowType.conditionalFlow;
    this.expression.visible = this.flowType.value === flowType.conditionalFlow;
  }

  updateProperties() {
    const element = this.panel.getElement();
    const elementSource = element.source;

    // 更新流转条件
    let conditionExpression;
    if (this.flowType.value === flowType.normalFlow) {
      conditionExpression = undefined;
    } else if (this.flowType.value === flowType.defaultFlow) {
      this.root.getModeling().updateProperties(elementSource, {
        default: element,
      });
    } else if (this.flowType.value === flowType.conditionalFlow) {
      conditionExpression = this.root.getModdle().create('bpmn:FormalExpression', {
        body: this.expression.value,
      });
    } else {
      throw new Error('不支持的流转类型');
    }

    this.root.getModeling().updateProperties(element, {
      conditionExpression,
    });
  }

  init() {
    this.initEvent();

    this.controls.forEach(it => {
      it.bindBpmnService(this.root);
      it.bindPanelService(this.panel);
    });

    const element = this.panel.getElement();
    const elementSourceRef = element.businessObject.sourceRef;

    // 开始节点联线不能设置流转条件
    this.visible = element.source?.type !== 'bpmn:StartEvent';

    if (elementSourceRef && elementSourceRef.default && elementSourceRef.default.id === element.id) {
      this.flowType.value = flowType.defaultFlow;
    } else if (element.businessObject.conditionExpression) {
      this.flowType.value = flowType.conditionalFlow;
      this.expression.value = element.businessObject.conditionExpression?.body;
    } else {
      this.flowType.value = flowType.normalFlow;
    }
    this.updateVisible();
  }

  onShow(){
    // 无操作
  }
}

export default FlowCardService;
